import {Buffer} from "buffer";
import {ActionRequest} from "./ActionRequest";
import {ActionRequestList} from "./ActionRequestList";
import {ActionThenGetRequestNormalList} from "./ActionThenGetRequestNormalList";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";

enum Choice {
    _ERR_NONE_SELECTED = -1,
    ACTION_REQUEST = 1,
    ACTION_REQUEST_LIST = 2,
    ACTION_THEN_GET_REQUEST_NORMAL_LIST = 3,
}

export class Action_Request implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    actionRequest : ActionRequest | null = null;
    actionRequestList : ActionRequestList | null = null;
    actionThenGetRequestNormalList : ActionThenGetRequestNormalList | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setActionRequest(actionRequest: ActionRequest) {
        this.resetChoices();
        this.choice = Choice.ACTION_REQUEST;
        this.actionRequest = actionRequest;
    }

    setActionRequestList(actionRequestList : ActionRequestList){
        this.resetChoices();
        this.choice = Choice.ACTION_REQUEST_LIST;
        this.actionRequestList = actionRequestList;
    }

    setActionThenGetRequestNormalList(actionThenGetRequestNormalList : ActionThenGetRequestNormalList){
        this.resetChoices();
        this.choice = Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST;
        this.actionThenGetRequestNormalList = actionThenGetRequestNormalList;
    }

    decode(input: ReverseByteArrayInputStream): number {

        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.ACTION_REQUEST) {
            this.actionRequest = new ActionRequest();
            codeLength += this.actionRequest.decode(input);
            return codeLength;
        }else if (this.choice == Choice.ACTION_REQUEST_LIST) {
            this.actionRequestList = new ActionRequestList();
            codeLength += this.actionRequestList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            this.actionThenGetRequestNormalList = new ActionThenGetRequestNormalList();
            codeLength += this.actionThenGetRequestNormalList.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }

        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            codeLength += this.actionThenGetRequestNormalList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(3);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.ACTION_REQUEST_LIST) {
            codeLength += this.actionRequestList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(2);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.ACTION_REQUEST) {
            codeLength += this.actionRequest.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(1)
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.actionRequest = null;
        this.actionRequestList = null;
        this.actionThenGetRequestNormalList = null
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.choice == Choice.ACTION_REQUEST) {
            if (this.actionRequest != null) {
                return "choice: {actionRequest: " + this.actionRequest + "}";
            } else {
                return "choice is actionRequest but actionRequest is null";
            }
        }else if(this.choice == Choice.ACTION_REQUEST_LIST){
            if(this.actionRequestList != null){
                return "choice: {actionRequestList: " + this.actionRequestList + "}";
            }else{
                return "choice is actionRequestList but actionRequestList is null";
            }
        }else if(this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST){
            if(this.actionThenGetRequestNormalList != null){
                return "choice: {actionThenGetRequestNormalList: " + this.actionThenGetRequestNormalList + "}";
            }else{
                return "choice is actionThenGetRequestNormalList but actionThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

}
